

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/pages/common_page/common_video.dart';
import 'package:moonplay/utils/common_utils.dart';
import 'package:moonplay/widgets/common_widget.dart';

class SetTingPage extends StatefulWidget {
  final dynamic arguments;

  const SetTingPage({Key key, this.arguments}) : super(key: key);

  @override
  _SetTingPageState createState() => _SetTingPageState();
}

class _SetTingPageState extends State<SetTingPage> {

  TextEditingController _startTimeController = TextEditingController();
  TextEditingController _endTimeController = TextEditingController();
  bool _isJump = false;
  bool _isNext = true;
  String _startTime;
  String _endTime;
  FocusNode _startTimeFocusNode = FocusNode();
  FocusNode _endTimeFocusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _startTime = SpUtil.getString('startTime');
    _endTime = SpUtil.getString('endTime');
    _startTimeController.text = SpUtil.getString('startTime');
    _endTimeController.text = SpUtil.getString('endTime');
    _startTimeFocusNode.addListener(() {
      if (!_startTimeFocusNode.hasFocus) {
        SpUtil.putString("startTime", _startTime);
      }
    });
    _endTimeFocusNode.addListener(() {
      if (!_endTimeFocusNode.hasFocus) {
        SpUtil.putString("endTime", _endTime);
      }
    });

    _isJump = SpUtil.getInt('isJump') == 1 ? true : false;


  }

  @override
  void dispose() {
    _startTimeFocusNode.unfocus();
    _endTimeFocusNode.unfocus();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: commonAppbar(context, title: '设置'),
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: ListView(
          children: <Widget>[
            // CommonListPage(
            //   leftText: Cptext(text: '跳过片头片尾'),
            //   rightText: _widgetSwitch(),
            // ),
            // _isJump
            // ? CommonListPage(
            //   leftText: Row(
            //     children: <Widget>[
            //       Cptext(text: '跳过片头时间'),
            //       Cptext(text: '(如1分50秒即输入150)', style: TextStyle(fontSize: 12, color: Color(0xff666666)),),
            //     ],
            //   ),
            //   rightText: _widgetStartTime(),
            // ) : Container(),
            // _isJump
            // ? CommonListPage(
            //   leftText: Row(
            //     children: <Widget>[
            //       Cptext(text: '跳过片尾时间'),
            //       Cptext(text: '(如1分50秒即输入150)', style: TextStyle(fontSize: 12, color: Color(0xff666666)),),
            //     ],
            //   ),
            //   rightText: _widgetEndTime(),
            // ) : Container(),
            // CommonListPage(
            //   leftText: Cptext(text: '自动播放下一集'),
            //   rightText: _widgetSwitchNext(),
            // ),
            CommonListPage(
              leftText: Cptext(text: '关于我们'),
              onTap: () async {

                // String _url = await getVideoUrl("http://www.iqiyi.com/v_23v9vzm4ot4.html");
                // LogUtil.e(_url);
              },
            ),
            // CommonListPage(
            //   leftText: Cptext(text: '打开webview'),
            //   onTap: (){
            //     Get.toNamed("/CommonWebviewPage");
            //   },
            // ),
          ],
        ),
      )
      
    );
  }

  Widget _widgetSwitch(){
    return Switch(
      value: _isJump,
      activeColor: Colors.blue,     // 激活时原点颜色
      onChanged: (bool val) {
        setState(() {
          _isJump = !_isJump;
        });
        if(val){
          SpUtil.putInt('isJump', 1);
        } else {
          SpUtil.putInt('isJump', 2);
        }
      },
    );
  }

  Widget _widgetSwitchNext(){
    return Switch(
      value: _isNext,
      activeColor: Colors.blue,     // 激活时原点颜色
      onChanged: (bool val) {
        setState(() {
            _isNext = !_isNext;
        });
      },
    );
  }

  Widget _widgetStartTime(){
    return Container(
      width: ScreenUtil().getWidthPx(120),
      height: 40,
      constraints: BoxConstraints(
        minWidth: ScreenUtil().getWidthPx(12)
      ),
      child: TextField(
        focusNode: _startTimeFocusNode,
        keyboardType: TextInputType.number,//键盘类型，数字键盘
        style: TextStyle(fontSize: ScreenUtil().getWidthPx(28), color: Theme.of(context).primaryColor),//输入文字样式
        controller: _startTimeController,//控制器
        decoration: InputDecoration(
            hintText: '请输入',
            hintStyle: TextStyle(fontSize: ScreenUtil().getWidthPx(28)),
            border: InputBorder.none,
        ),
        inputFormatters: <TextInputFormatter>[
            WhitelistingTextInputFormatter.digitsOnly,//只输入数字
            LengthLimitingTextInputFormatter(3)//限制长度
        ],
        onChanged: (val){
          setState(() {
            _startTime = val;
          });
        },
      ),
    );
  }

  Widget _widgetEndTime(){
    return Container(
      width: ScreenUtil().getWidthPx(120),
      height: 40,
      constraints: BoxConstraints(
        minWidth: ScreenUtil().getWidthPx(12)
      ),
      child: TextField(
        focusNode: _endTimeFocusNode,
        keyboardType: TextInputType.number,//键盘类型，数字键盘
        style: TextStyle(fontSize: ScreenUtil().getWidthPx(28), color: Theme.of(context).primaryColor),//输入文字样式
        controller: _endTimeController,//控制器
        decoration: InputDecoration(
            hintText: '请输入',
            hintStyle: TextStyle(fontSize: ScreenUtil().getWidthPx(28)),
            border: InputBorder.none,
        ),
        inputFormatters: <TextInputFormatter>[
            WhitelistingTextInputFormatter.digitsOnly,//只输入数字
            LengthLimitingTextInputFormatter(3)//限制长度
        ],
        onChanged: (val){
          setState(() {
            _endTime = val;
          });
        },
      ),
    );
  }

  void getHttp() async {
    try {
      Response response = await Dio().get("http://v.juhe.cn/toutiao/index?type=top&key=1787e372703ef15365c2cda90b51de36");
      print(response);
      if(response != null) {
        CpToast('成功获取到了${response.data['reason']}');
      }
    } catch (e) {
      print(e);
    }
  }
}



// RaisedButton(
            //     onPressed: (){
            //       Provide.value<ThemeProvider>(context).setTheme(ThemeMode.dark);
            //     },
            //     child: Text('切换夜间主题'),
            //   ),
            //   RaisedButton(
            //     onPressed: (){
            //       // Provide.value<ThemeColor>(context).setAppColor('appWhiteColors');
            //       Provide.value<ThemeProvider>(context).setTheme(ThemeMode.light);
            //     },
            //     child: Cptext(text: '切换日间主题',),
            //   ),
            //   RaisedButton(
            //     onPressed: (){
            //       // Provide.value<ThemeColor>(context).setAppColor('appWhiteColors');
            //       Provide.value<ThemeProvider>(context).setTheme(ThemeMode.system);
            //     },
            //     child: Cptext(text: '跟随系统主题',),
            //   ),
            //   RaisedButton(
            //     onPressed: (){
            //       EventBus.instance.commit(EventKeys.SwitchLanguage,{"data": {"currentSupportLocale": 2}});
            //     },
            //     child: Cptext(text: '切换英文',),
            //   ),
            //   RaisedButton(
            //     onPressed: (){
            //       EventBus.instance.commit(EventKeys.SwitchLanguage,{"data": {'currentSupportLocale': 1}});
            //     },
            //     child: Cptext(text: '切换中文${S.of(context).switch_language}',),
            //   ),
            //   RaisedButton(
            //     onPressed: (){
            //       EventBus.instance.commit(EventKeys.SwitchLanguage,{"data": {'currentSupportLocale': 3}});
            //     },
            //     child: Cptext(text: '切换香港${S.of(context).switch_language}',),
            //   ),
            //   Container(
            //     child: Cptext(text: '测试切换主题色时文字颜色更改${S.of(context).switch_language}${_count}',),
            //   ),
            //   CommonStyle.bottomLine()